<?php

namespace App\Model;

use App\Http\Controllers\Constant\Constant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
 * @property integer $work_id
 * @property integer $spuid
 * @property integer $roleid
 * @property integer $pid
 * @property integer $status
 * @property string $note
 * @property string $created_at
 */
class OAShenPiLog extends Model
{
    protected $table = 'shenpi_log';

    public $timestamps = false;

    protected $fillable = [
        'work_id','spuid','roleid',
        'created_at','pid','note',
        'status'
    ];

    const SHENPI_STATUS_NAMES = ['<font style="color: red;">拒绝</font>', '<font style="color: green;">通过</font>', '记录','无'];
    const SHENPI_STATUS_REFUSE = 0;//拒绝
    const SHENPI_STATUS_PASS = 1;//通过
    const SHENPI_STATUS_LOG = 2;//记录
    const SHENPI_STATUS_NOTHING = 3;//无


    public static function getChangeLogByRoleid($project,$roleid)
    {
        return self::query()
            ->where([
                'pid'=>$project->id,
                'work_id'=>$project->work_id,
                'roleid'=>$roleid
            ])
            ->where('status','<>',self::SHENPI_STATUS_NOTHING)
            ->exists();
    }

    public static function createShenPiLogs($workId,$pid,$note,$status=Constant::PROJECT_STATUS_WAIT)
    {
        if(is_bool($note)) return null;
        $note = is_array($note) ? implode(';',$note) : $note;
        $data = [
            'work_id'=>$workId,
            'spuid'=>admin_id(),
            'roleid'=>roleid(),
            'pid'=>$pid,
            'note'=>$note,
            'status'=>$status,
            'created_at' => date('Y-m-d H:i:s'),
        ];
        if($status== self::SHENPI_STATUS_NOTHING){
            return self::query()->updateOrCreate([
                'work_id'=>$workId,
                'pid'=>$pid,
                'spuid'=>admin_id(),
                'roleid'=>roleid(),
                'status'=>$status,
            ],$data);
        }else{
            return self::query()->create($data);
        }
    }

    public static function checkShenLogExists($workId,$pid,$status=0)
    {
        return self::query()
                    ->where('work_id',$workId)
                    ->where('spuid',admin_id())
                    ->where('roleid',roleid())
                    ->where('pid',$pid)
                    ->when($status,function ($query) use ($status) {
                        $query->where('status',$status);
                    })
                    ->exists();
    }

    //获取临时存储的审批备注
    public static function getNothingShenPiLog($workId,$pid)
    {
        return self::
            where('work_id', $workId)
            ->where('spuid', admin_id())
            ->where('roleid', roleid())
            ->where('pid', $pid)
            ->where('status', self::SHENPI_STATUS_NOTHING)
            ->first();
    }

    //获取临时存储的审批备注以及附件
    public static function getTempSpFormData($workId,$pid)
    {
        $log = self::getNothingShenPiLog($workId,$pid);
        $spFile = $workId == OAWork::WORK_PROJECT_LX ? [] : ($log ? OAFiles::getProjectFileList($workId,$log->id,OAFiles::FILE_TYPE_TEMP) : []);
        return [
            'note'=>optional($log)->note ?? '',
            'fileList'=>$spFile,
        ];
    }


}
